/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uniscol.dao.impl;

import fr.uniscol.dao.IOptionDao;
import fr.uniscol.dao.UniscolDaoSupport;
import static fr.uniscol.dao.impl.SQLNames.*;
import fr.uniscol.dao.mapper.OptionRowMapper;
import fr.uniscol.model.Option;
import fr.uniscol.model.Scolarite;
import java.util.List;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Christophe
 */
@Repository("optionDao")
public class OptionDaoImpl extends UniscolDaoSupport implements IOptionDao {

    @Override
    public List<Option> getAllOptionsOfScolarite(Scolarite scolarite) {
        
        List<Option> options = getJdbcTemplate().query("SELECT * FROM " + TABLE_OPTION_ELEVE + " o, " + TABLE_MATIERE + " m "
                                                    + "WHERE o.matiereid=m.matiereid "
                                                    + "AND o.scolariteid=? ", new Object[]{
                                                        scolarite.getId()
                                                    },
                                                    new OptionRowMapper());
        return options;
    }
    
    @Override
    public boolean deleteAllOptionsOfScolarite(Scolarite scolarite){
        
        int nbRows = getJdbcTemplate().update("DELETE FROM " + TABLE_OPTION_ELEVE + " WHERE scolariteid=? ", new Object[]{
            scolarite.getId()
        });
        
        return true;
    }

    @Override
    public boolean saveOption(Scolarite scolarite, Option option) {
        
        if(scolarite == null || option == null){
            return false;
        }
        
        int nbRowsAffected = getJdbcTemplate().update("INSERT INTO " + TABLE_OPTION_ELEVE + " (scolariteid, matiereid, modalite) VALUES(?,?,?)", new Object[]{
           scolarite.getId(),
           option.getMatiere().getId(),
           option.getModalite()
        });
        
        return nbRowsAffected == 1;
    }
}
